﻿using Castle.Windsor;
using Castle.MicroKernel.Registration;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Cfg;
using NHibernate.Validator.Cfg.Loquacious;
using uNhAddIns.Adapters;
using uNhAddIns.NHibernateValidator;
using uNhAddIns.NHibernateTypeResolver;
using MM.Persistence.NH.Constraints;

namespace MM.Bootstrapper.Configurators
{
    public class NHVConfigurator : IConfigurator
    {
        #region Miembros de IConfigurator

        public void Configure(IWindsorContainer container)
        {

            var ve = new ValidatorEngine();

            container.Register(Component.For<IEntityValidator>()
                                        .ImplementedBy<EntityValidator>()
                                );

            container.Register(Component.For<ValidatorEngine>()
                                        .Instance(ve)
                                        .LifeStyle.Singleton
                              );

            container.Register(Component.For<ISharedEngineProvider>()
                                .ImplementedBy<NHVSharedEngineProvider>()
                                );

            Environment.SharedEngineProvider = container.Resolve<ISharedEngineProvider>();

            var configure = new FluentConfiguration();

            configure.Register(typeof(MovieValidationDef).Assembly.ValidationDefinitions())
            .SetDefaultValidatorMode(ValidatorMode.OverrideAttributeWithExternal)
            .AddEntityTypeInspector<NHVTypeInspector>()
            .IntegrateWithNHibernate.ApplyingDDLConstraints().And.RegisteringListeners();

            ve.Configure(configure);
            
            
        }

        #endregion
    }
}
